package com.ruoyi.web.controller.admin.newSystem;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.permission.vo.permission.PermissionAssignRoleDataScopeReqVO;
import com.ruoyi.common.core.domain.permission.vo.permission.PermissionAssignRoleMenuReqVO;
import com.ruoyi.common.core.domain.permission.vo.permission.PermissionAssignUserRoleReqVO;
import com.ruoyi.newSystem.bean.output.AuthPermissionInfoRespVO;
import com.ruoyi.newSystem.service.PermissionService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;


/**
 * 权限 Controller，提供赋予用户、角色的权限的 API 接口
 *
 * @author 后台源码
 */
@Tag(name = "管理后台 - 权限")
@RestController
@RequestMapping("/newSystem/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    /**
     * 设置角色菜单
     *
     * @param reqVO the req vo
     * @return the common result
     */
    @PostMapping("/assignRoleMenu")
    public AjaxResult assignRoleMenu(@Validated @RequestBody PermissionAssignRoleMenuReqVO reqVO) {
        // 执行菜单的分配
        permissionService.assignRoleMenu(reqVO.getRoleId(), reqVO.getMenuIds());
        return success(true);
    }

    /**
     * 获得角色拥有的菜单编号集合
     *
     * @param roleId the role id
     * @return the role menu list
     */
    @GetMapping("/getRoleMenuListByRoleId")
    public AjaxResult getRoleMenuListByRoleId(Long roleId) {
        return success(permissionService.getRoleMenuListByRoleId(roleId));
    }

    /**
     * 设置角色的数据权限
     *
     * @param reqVO the req vo
     * @return the common result
     */
    @PostMapping("/assignRoleDataScope")
    public AjaxResult assignRoleDataScope(@Valid @RequestBody PermissionAssignRoleDataScopeReqVO reqVO) {
        permissionService.assignRoleDataScope(reqVO.getRoleId(), reqVO.getDataScope(), reqVO.getDataScopeDeptIds());
        return success(true);
    }

    @PostMapping("/assignRoleDataSet")
    public AjaxResult assignRoleDataSet(@Valid @RequestBody PermissionAssignRoleDataScopeReqVO reqVO) {
        permissionService.assignRoleDataset(reqVO.getRoleId(), reqVO.getDataSetIds());
        return success(true);
    }

    /**
     * 获得用户拥有的角色编号集合
     *
     * @param userId the user id
     * @return the common result
     */
    @GetMapping("/getUserRoleIdListByUserId")
    public AjaxResult getUserRoleIdListByUserId(@RequestParam("userId") Long userId) {
        return success(permissionService.getUserRoleIdListByUserId(userId));
    }

    /**
     * 获得用户拥有的角色编号集合，从缓存中获取
     *
     * @param userId the user id
     * @return the common result
     */
    @GetMapping("/getUserRoleIdListByUserIdFromCache")
    public AjaxResult getUserRoleIdListByUserIdFromCache(@RequestParam("userId") Long userId) {
        return success(permissionService.getUserRoleIdListByUserIdFromCache(userId));
    }

    /**
     * 设置用户角色
     *
     * @param reqVO the req vo
     * @return the common result
     */
    @PostMapping("/assignUserRole")
    public AjaxResult assignUserRole(@Validated @RequestBody PermissionAssignUserRoleReqVO reqVO) {
        permissionService.assignUserRole(reqVO.getUserId(), reqVO.getRoleIds());
        return success(true);
    }

    @PostMapping("/assignUserDept")
    public AjaxResult assignUserDept(@Validated @RequestBody PermissionAssignUserRoleReqVO reqVO) {
        permissionService.assignUserDept(reqVO.getUserId(), reqVO.getDeptIds());
        return success(true);
    }

    /**
     * 系统管理菜单
     *
     * @return
     */
    @GetMapping("/buildMenuTreeSystem")
    public AjaxResult buildMenuTreeSystem() {
        List<AuthPermissionInfoRespVO.MenuVO> list = permissionService.buildMenuTreeSystem();
        // 排序后，返回给前端
        return success(list);
    }

    /**
     * 智能应用
     *
     * @return
     */
    @GetMapping("/buildMenuTreeSelect")
    public AjaxResult buildMenuTreeSelect() {
        List<AuthPermissionInfoRespVO.MenuVO> list = permissionService.buildMenuTreeSelect();

        return success(list);
    }

    /**
     * 构建知识权限范围的树形结构
     *
     */
    @GetMapping("/buildTreeKnowledge")
    public AjaxResult buildTreeKnowledge() {
        return success(permissionService.buildTreeKnowledge());
    }
}
